package com.sunday.aomicaichang.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.base.BaseFragment;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.aomicaichang.model.OrderNum;
import com.sunday.aomicaichang.ui.collection.CollectionActivity;
import com.sunday.aomicaichang.ui.common.WebViewActivity;
import com.sunday.aomicaichang.ui.coupon.CouponActivity;
import com.sunday.aomicaichang.ui.my.AddressListActivity;
import com.sunday.aomicaichang.ui.my.MyIntegralActivity;
import com.sunday.aomicaichang.ui.my.OwnerDataActivity;
import com.sunday.aomicaichang.ui.my.WalletActivity;
import com.sunday.aomicaichang.ui.order.BuyOrderListActivity;
import com.sunday.aomicaichang.ui.recharge.SelectRechargeActivity;
import com.sunday.member.entity.Member;
import com.sunday.member.utils.SignUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/5/22.
 */

public class MineFragment extends BaseFragment {

    @Bind(R.id.title_bar)
    RelativeLayout titleBar;
    @Bind(R.id.img)
    ImageView img;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.phone)
    TextView phone;
    @Bind(R.id.ly_my)
    RelativeLayout lyMy;
    @Bind(R.id.ly_all_order)
    RelativeLayout lyAllOrder;
    @Bind(R.id.btn_pay)
    TextView btnPay;
    @Bind(R.id.btn_go)
    TextView btnGo;
    @Bind(R.id.btn_to)
    TextView btnTo;
    @Bind(R.id.btn_assess)
    TextView btnAssess;
    @Bind(R.id.gridview)
    GridView gridview;
    private List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
    private SimpleAdapter adapter;
    private int[] icon = {R.mipmap.user_order_collection, R.mipmap.user_order_coupon,
            R.mipmap.recharge,R.mipmap.user_order_integral,
            R.mipmap.user_order_purse,R.mipmap.user_order_address,R.mipmap.recharge};

    private String[] iconName = {"我的收藏", "我的优惠券","会员充值",
            "我的积分","我的余额","收货地址","我的会员卡"};

    private Member member;

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_my, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDataNum();
        getData();
        initView();
    }

    public List<Map<String, Object>> getData() {
        for (int i = 0; i < icon.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            dataList.add(map);
        }
        return dataList;
    }


    private void initView() {
        String[] from = {"image", "text"};
        int[] to = {R.id.item_image, R.id.item_text};
        adapter = new SimpleAdapter(mContext, dataList, R.layout.item_my, from, to);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0://我的收藏
                        intent = new Intent(mContext, CollectionActivity.class);
                        startActivity(intent);
                        break;
                    case 1://我的优惠券
                        intent = new Intent(mContext, CouponActivity.class);
                        startActivity(intent);
                        break;
                    case 2://会员充值
                        intent = new Intent(mContext, SelectRechargeActivity.class);
                        startActivity(intent);
                        break;
                    case 3://我的积分
                        intent = new Intent(mContext, MyIntegralActivity.class);
                        startActivity(intent);
                        break;
                    case 4://我的余额
                        intent = new Intent(mContext, WalletActivity.class);
                        startActivity(intent);
                        break;
                    case 5://收货地址
                        intent = new Intent(mContext, AddressListActivity.class);
                        startActivity(intent);
                        break;
                    case 6://我的会员卡
                        intent = new Intent(mContext, WebViewActivity.class);
                        String url="http://mobile.51huaguoshan.com/sunday/mobile/member/card/index?memberId="+member.getId();
                        intent.putExtra("title","我的会员卡");
                        intent.putExtra("url",url);
                        startActivity(intent);
                        break;
                }
            }
        });

        member = (Member) SharePerferenceUtils.getIns(mContext).getOAuth();
        if (!TextUtils.isEmpty(member.getImage())) {
            Picasso.with(mContext)
                    .load(member.getImage())
                    .error(R.mipmap.ic_avator)
                    .resize(130, 130)
                    .into(img);
        }
        name.setText("用户名:  "+member.getNickName());
        String phones= member.getMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
        phone.setText("登录手机号:  "+phones);

    }

    @Override
    public void onResume() {
        super.onResume();
        member = (Member) SharePerferenceUtils.getIns(mContext).getOAuth();
        if (!TextUtils.isEmpty(member.getImage())) {
            Picasso.with(mContext)
                    .load(member.getImage())
                    .error(R.mipmap.ic_avator)
                    .resize(130, 130)
                    .into(img);
        }
        name.setText("用户名:  "+member.getNickName());
        String phones= member.getMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
        phone.setText("登录手机号:  "+phones);
        getDataNum();
    }

    @OnClick({R.id.ly_my,R.id.btn_pay, R.id.btn_go, R.id.btn_assess, R.id.btn_to,R.id.ly_all_order})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.ly_my:
                intent = new Intent(mContext, OwnerDataActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_pay:
                intent = new Intent(mContext, BuyOrderListActivity.class);
                intent.putExtra("page", 1);
                startActivity(intent);
                break;
            case R.id.btn_go:
                intent = new Intent(mContext, BuyOrderListActivity.class);
                intent.putExtra("page", 2);
                startActivity(intent);
                break;
            case R.id.btn_to:
                intent = new Intent(mContext, BuyOrderListActivity.class);
                intent.putExtra("page", 3);
                startActivity(intent);
                break;
            case R.id.btn_assess:
                intent = new Intent(mContext, BuyOrderListActivity.class);
                intent.putExtra("page", 4);
                startActivity(intent);
                break;
            case R.id.ly_all_order:
                intent = new Intent(mContext, BuyOrderListActivity.class);
                intent.putExtra("page", 0);
                startActivity(intent);
                break;
        }
    }
    private void getDataNum() {
        String nonce= MD5Utils.random();
        String[] str1=new String[]{"nonce"};
        String[] str2=new String[]{nonce};
        Call<ResultDO<OrderNum>> call= ApiClient.getApiAdapter().getOrderNumber(nonce, SignUtil.getSign(str1,str2));
        call.enqueue(new Callback<ResultDO<OrderNum>>() {
            @Override
            public void onResponse(Call<ResultDO<OrderNum>> call, Response<ResultDO<OrderNum>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    if (response.body().getResult() == null) {
                        return;
                    }
                    OrderNum orderNum=response.body().getResult();
                    if(orderNum.getDfk()>0) {
                        Badge badge1 = new QBadgeView(mContext).bindTarget(btnPay).setBadgeNumber(orderNum.getDfk());
                        badge1.setShowShadow(false);
                        badge1.setBadgeBackground(ContextCompat.getDrawable(mContext,R.color._ed6d00),true);
                        badge1.setBadgeTextSize(10,true);
                    }
                    if(orderNum.getDps()>0) {
                        Badge badge2 = new QBadgeView(mContext).bindTarget(btnGo).setBadgeNumber(orderNum.getDps());
                        badge2.setShowShadow(false);
                        badge2.setBadgeBackground(ContextCompat.getDrawable(mContext,R.color._ed6d00),true);
                        badge2.setBadgeTextSize(10,true);
                    }
                    if(orderNum.getPsz()>0) {
                        Badge badge3 = new QBadgeView(mContext).bindTarget(btnTo).setBadgeNumber(orderNum.getPsz());
                        badge3.setShowShadow(false);
                        badge3.setBadgeBackground(ContextCompat.getDrawable(mContext,R.color._ed6d00),true);
                        badge3.setBadgeTextSize(10,true);
                    }
                    if(orderNum.getDpj()>0) {
                        Badge badge4 = new QBadgeView(mContext).bindTarget(btnAssess).setBadgeNumber(orderNum.getDpj());
                        badge4.setShowShadow(false);
                        badge4.setBadgeBackground(ContextCompat.getDrawable(mContext,R.color._ed6d00),true);
                        badge4.setBadgeTextSize(10,true);
                    }
                }
            }
            @Override
            public void onFailure(Call<ResultDO<OrderNum>> call, Throwable t) {
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
